package com.ymatou.query.seller.trading.api.parameter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;

/**
 * 存储过程: sp_trd_GetSellerOderInfoByProductId
 * 
 * @author songyefeng
 *
 */
public class Sp_GetSellerOrderInfoByProductId {
	private Integer sellerId;
	private Integer orderEstablishStatus;
	private String productId;
	private List<Integer> orderStatus;
	private Boolean considerOrderStatus;
	private Boolean considerRCOrderEstablish;
	private Boolean considerRCAccountPaid;
	private Boolean considerRestOrderStatus;

	public Integer getSellerId() {
		return sellerId;
	}

	public void setSellerId(Integer sellerId) {
		this.sellerId = sellerId;
	}

	public Integer getOrderEstablishStatus() {
		return orderEstablishStatus;
	}

	public void setOrderEstablishStatus(Integer orderEstablishStatus) {
		this.orderEstablishStatus = orderEstablishStatus;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public List<Integer> getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(List<Integer> orderStatus) {
		this.orderStatus = orderStatus;
	}

	public Boolean getConsiderOrderStatus() {
		return considerOrderStatus;
	}

	public void setConsiderOrderStatus(Boolean considerOrderStatus) {
		this.considerOrderStatus = considerOrderStatus;
	}

	public Boolean getConsiderRCOrderEstablish() {
		return considerRCOrderEstablish;
	}

	public void setConsiderRCOrderEstablish(Boolean considerRCOrderEstablish) {
		this.considerRCOrderEstablish = considerRCOrderEstablish;
	}

	public Boolean getConsiderRCAccountPaid() {
		return considerRCAccountPaid;
	}

	public void setConsiderRCAccountPaid(Boolean considerRCAccountPaid) {
		this.considerRCAccountPaid = considerRCAccountPaid;
	}

	public Boolean getConsiderRestOrderStatus() {
		return considerRestOrderStatus;
	}

	public void setConsiderRestOrderStatus(Boolean considerRestOrderStatus) {
		this.considerRestOrderStatus = considerRestOrderStatus;
	}

	private List<Ymt_Order> orderList = new ArrayList<Ymt_Order>();
	private List<Ymt_OrderInfo> orderInfoList = new ArrayList<Ymt_OrderInfo>();
	private List<Ymt_Users> userList = new ArrayList<Ymt_Users>();

	public List<Ymt_Order> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<Ymt_Order> orderList) {
		this.orderList = orderList;
	}

	public List<Ymt_OrderInfo> getOrderInfoList() {
		return orderInfoList;
	}

	public void setOrderInfoList(List<Ymt_OrderInfo> orderInfoList) {
		this.orderInfoList = orderInfoList;
	}

	public List<Ymt_Users> getUserList() {
		return userList;
	}

	public void setUserList(List<Ymt_Users> userList) {
		this.userList = userList;
	}

	private DefaultWapper defaultWapper = new DefaultWapper();
	private List<Map> infos = new ArrayList<Map>();
	private Map summary = new HashMap();

	public List<Map> getInfos() {
		return infos;
	}

	public Map getSummary() {
		return summary;
	}

	@SuppressWarnings("rawtypes")
	public void Execute() {
		String queryField = " oi.sCatalogId as catalogId,";
		queryField += " max(oi.dAddTime) as lastOrderTime,";
		queryField += " count(oi.sCatalogId) as orderCount,";
		queryField += " sum(case when o.iTradingStatus = "
				+ orderEstablishStatus
				+ " then oi.iAmount else 0 end) as notPaidCount,";
		queryField += " sum(case when o.iTradingStatus = "
				+ orderEstablishStatus
				+ " then 1 else 0 end) as notPaidOrderCount,";
		queryField += " sum(case when isnull(o.iTradingStatus, 0) != "
				+ orderEstablishStatus
				+ " then oi.iAmount else 0 end) as soldCount,";
		queryField += " sum(case when isnull(o.iTradingStatus, 0) != "
				+ orderEstablishStatus
				+ " then 1 else 0 end) as soldOrderCount";
		String joinCondition = " join Ymt_Orders(nolock) o on o.iOrderId = oi.iOrderId";
		String queryCondition = " o.iBuyerId = " + sellerId;
		queryCondition += " and oi.sProductId = '" + productId + "'";
		if (DataUtil.isNullOrEmpty(orderStatus) == false) {
			queryCondition += " and o.iTradingStatus in "
					+ DataUtil.toInOfSql(orderStatus);
		}
		queryCondition += " group by oi.sCatalogId";
		infos = defaultWapper.selectOrderInfo(queryField, joinCondition,
				queryCondition);

		summary.put("lastOrderTime",
				String.valueOf(infos
						.stream()
						.max((x, y) -> ((java.sql.Timestamp) y
								.get("lastOrderTime"))
								.compareTo(((java.sql.Timestamp) x
										.get("lastOrderTime")))).get()
						.get("lastOrderTime")));
		summary.put(
				"notPaidCount",
				infos.stream()
						.max((x, y) -> (Integer) y.getOrDefault("notPaidCount",
								0)
								- (Integer) x.getOrDefault("notPaidCount", 0))
						.get().get("notPaidCount"));
		summary.put(
				"notPaidOrderCount",
				infos.stream()
						.max((x, y) -> (Integer) y.getOrDefault(
								"notPaidOrderCount", 0)
								- (Integer) x.getOrDefault("notPaidOrderCount",
										0)).get().get("notPaidOrderCount"));
		summary.put(
				"soldCount",
				infos.stream()
						.max((x, y) -> (Integer) y.getOrDefault("soldCount", 0)
								- (Integer) x.getOrDefault("soldCount", 0))
						.get().get("soldCount"));
		summary.put(
				"soldOrderCount",
				infos.stream()
						.max((x, y) -> (Integer) y.getOrDefault(
								"soldOrderCount", 0)
								- (Integer) x.getOrDefault("soldOrderCount", 0))
						.get().get("soldOrderCount"));
	}
}
